package com.mysum.netty.server;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.frame.FrameDecoder;

public class MessageDecoder extends FrameDecoder {  
	   
    @Override  
    protected Object decode(  
            ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception {  
        if (buffer.readableBytes() < 4) {  
            return null;//(1)  
        }  
        System.out.println("buffer.readerIndex()" + buffer.readerIndex());
        System.out.println("buffer.getInt(buffer.readerIndex())" + buffer.getInt(buffer.readerIndex()));
        int dataLength = buffer.getInt(buffer.readerIndex());  
        if (buffer.readableBytes() < dataLength + 4) {  
            return null;//(2)  
        }  
   
        buffer.skipBytes(4);//(3)  
        byte[] decoded = new byte[dataLength];  
        buffer.readBytes(decoded);  
        String msg = new String(decoded);//(4)  
        return msg;  
    }  
}  